package com.genericdevice.platform.core.mvp;

import android.app.Activity;
import com.genericdevice.platform.core.define.EventConstants;
import com.genericdevice.platform.core.event.IEvent;
import com.genericdevice.platform.core.event.listeners.CreateListener;
import com.genericdevice.platform.core.event.EventBus;
import com.genericdevice.platform.core.event.listeners.LoadListener;

/**
 * Created by Xilent on 17.05.2014.
 */
public abstract class AbstractPresenter<View extends IView> {
    protected Activity activity;

    protected View view;

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void init() {
        view = createView();
        view.setActivity(activity);
        view.setPresenter(this);

        EventBus.addListener(EventConstants.PAGE_CREATE, new CreateListener(true) {
            @Override
            protected void onCreate(IEvent event) {
                AbstractPresenter.this.onCreate();
            }
        });

        EventBus.addListener(EventConstants.PAGE_LOAD, new LoadListener(true) {
            @Override
            protected void onLoad(IEvent event) {
                AbstractPresenter.this.onLoad();
            }
        });
    }

    protected void onCreate() {
        view.onCreate();

        EventBus.fireEvent(EventConstants.PAGE_LOAD);
    }

    protected void onLoad() {
        view.onLoad();
    }

    public void destory() {
        view.destroy();
    }

    protected abstract View createView();

}
